import {layuiCachedTable} from "./cache"
/**
 * @param that
 * @param editOradd
 * @param url
 * @param data
 * @param func 为空的时候就是默认的弹出框形式
 * @param func1 为空的时候就是默认的弹出框后就直接关闭layer index
 */
export function postTips(that, editOradd, url, data={}, func = null, func1 = null,z) {
    that.$post(url, data,that,z)
        .then((response) => {
            if (func == null) {
                if (response.data.res == 1) {//成功
                    allSuccess = 'success'
                    editOradd!=""?that.$message({message: editOradd+"成功！", type: "success"}):""
                    if (func1 != null) {
                        func1(response)
                    }
                } else {//失败
                    allSuccess = 'faild'
                    if (typeof response.data.data == "object") {
                        if (response.data.data[0] == undefined){
                            let t = Object.values(response.data.data);
                            if (typeof t == "string") {
                                editOradd != "" ? that.$message({
                                    message: editOradd + "失败！" + t,
                                    type: "error"
                                }) : ""
                            }else{
                                if (typeof t[0] == "string") {
                                    editOradd != "" ? that.$message({
                                        message: editOradd + "失败！" + t[0],
                                        type: "error"
                                    }) : ""
                                }else{
                                    editOradd != "" ? that.$message({
                                        message: editOradd + "失败！" + t[0][0],
                                        type: "error"
                                    }) : ""
                                }
                            }
                        }else{
                            if (typeof response.data.data[0] == "string") {
                                editOradd != "" ? that.$message({
                                    message: editOradd + "失败！" + response.data.data[0],
                                    type: "error"
                                }) : ""
                            }
                        }
                    } else {
                        editOradd != "" ? that.$message({message: editOradd + "失败！", type: "error"}) : ""
                    }
                }
            } else {
                allSuccess = 'success'
                func(response)
            }
        }).catch((response) => {
        allSuccess = 'faild'
        consoleLog(response)
    });
}
/**
 * 打开id的内容的窗口
 * @param id
 * @param endFunc 可以自己执行的函数
 * @returns {*}
 */
export function layOpen(id, endFunc = null,title="",area=['70%', '80%']) {
    return layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: area, //宽高
        close: true,
        title: title,
        shadeClose: true,
        content: $(id),
        end: () => {//关闭隐藏
            if (endFunc != null) {
                endFunc()
            }
        }
    });
}
/**
 * 简单的加载表格函数
 * @param url
 * @param id
 * @param datas
 * @param header
 * @param height
 * @param toolbar
 */
export function layTable(that, url, id, datas, header, height = 500, toolbar = '',page={layout: ['count']},done=null) {
    layuiCachedTable(that, {
        elem: id
        , url: url==""?"":(burl + url) //数据接口
        , toolbar: toolbar //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , where: datas
        , method: "post"
        , height: height
        , limits: [10, 30, 60, 90]
        , cols: [header]
        , page: page //开启分页
        , done:(res, curr, count)=>{
            if (done!=null) {
                done(res, curr, count);
            }
        }
    });
}